﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using Microsoft.Xna.Framework;
using System.Threading;

namespace WarPigs
{
    

    public class LocalPlayerData
    {


        public pmcData data;

        //Dialog window
        CreatePMCDialog createPMCDialog;

        public readonly string pmcFilename = "default.pmc";

        public LocalPlayerData(WarPigs game)
        {
            
            // Get the path of the save game
            string fullpath = pmcFilename;

            // Check to see if the save exists
            if (!File.Exists(fullpath))
            {
                //If the file doesn't exist, make a fake one, launch the creation dialog and save results
                // Create the data to save
                // CreatePMCData
                game.data = new pmcData();
                game.data.pmcName = " ";
                SavePmcData(game.data, pmcFilename);
                Thread.Sleep(5000);
                game.data.pmcName = "DarkWater";
                game.data.pmcColor = Color.DarkRed.ToString();
                game.data.pmcColorC = Color.DarkSlateBlue;
                SavePmcData(game.data, pmcFilename);
            }
            else
            {
                //Load the results from default.pmc
                game.data = LoadPmcData(fullpath);
            }
        }

        public static void SavePmcData(pmcData data, string filename)
        {
            // Get the path of the save game
            string fullpath = filename;

            // Open the file, creating it if necessary
            FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate);
            try
            {
                // Convert the object to XML data and put it in the stream
                XmlSerializer serializer = new XmlSerializer(typeof(pmcData));
                serializer.Serialize(stream, data);
            }
            finally
            {
                // Close the file
                stream.Close();
            }
        }

        public static pmcData LoadPmcData(string filename)
        {
            pmcData data;

            // Get the path of the save game
            string fullpath = filename;

            // Open the file
            FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate,
            FileAccess.Read);
            try
            {
                // Read the data from the file
                XmlSerializer serializer = new XmlSerializer(typeof(pmcData));
                data = (pmcData)serializer.Deserialize(stream);
            }
            finally
            {
                // Close the file
                stream.Close();
            }

            return (data);
        }
    }
}
